<?php
/*---------------------------------------------------------------------------
 小微OA系统 - 让工作更轻松快乐

 Copyright (c) 2013 http://www.smeoa.com All rights reserved.

 Author:  jinzhu.yin<smeoa@qq.com>

 Support: https://git.oschina.net/smeoa/xiaowei
 -------------------------------------------------------------------------*/

// 节点模型
namespace Home\Model;
use Think\Model;

class  PositionModel extends CommonModel {
    protected $_validate = array( array('name', 'checkNode', '节点已经存在', 0, 'callback'), );

    public function checkNode() {
        $map['name'] = $_POST['name'];
        $map['pid'] = isset($_POST['pid']) ? $_POST['pid'] : 0;

        if (!empty($_POST['id'])) {
            $map['id'] = array('neq', $_POST['id']);
        }
        $result = $this -> where($map) -> field('id') -> find();
        if ($result) {
            return false;
        } else {
            return true;
        }
    }

    public function get_sub_position_user_list($user_id) {
        $where1['user_id'] = array('eq', $user_id);
        $position_id = M("User") -> where($where1) -> getField('position_id');

        $where2['id'] = array('eq', $position_id);
        $position_sort = M('Position') -> where($where2) -> getField('sort');

        $sub_position = ceil($position_sort / 10 + 0.1) * 10;

        $where3['sort'] = array('egt', $sub_position);
        $sub_position_id_list = $this -> where($where3) -> getField('id', true);
 
        $where3['position_id'] = array('in', $sub_position_id_list);
        return M('User') -> where($where3) -> getField('id', true);
    }

}
?>